require 'rubygems'
require 'gosu'

path = File.dirname(__FILE__)
Dir.foreach path do |filename|
  require "#{path}/#{filename}" if filename.casecmp('gosub.rb') != 0 and filename =~ /\.rb$/i
end

module Gosu
  module Button
    SYM_TO_ID = {
      :number_0 => Kb0,
      :number_1 => Kb1,
      :number_2 => Kb2,
      :number_3 => Kb3,
      :number_4 => Kb4,
      :number_5 => Kb5,
      :number_6 => Kb6,
      :number_7 => Kb7,
      :number_8 => Kb8,
      :number_9 => Kb9,
      :backspace => KbBackspace,
      :delete => KbDelete,
      :down => KbDown,
      :end => KbEnd,
      :enter => KbEnter,
      :escape => KbEscape,
      :f1 => KbF1,
      :f10 => KbF10,
      :f11 => KbF11,
      :f12 => KbF12,
      :f2 => KbF2,
      :f3 => KbF3,
      :f4 => KbF4,
      :f5 => KbF5,
      :f6 => KbF6,
      :f7 => KbF7,
      :f8 => KbF8,
      :f9 => KbF9,
      :home => KbHome,
      :insert => KbInsert,
      :left => KbLeft,
      :left_alt => KbLeftAlt,
      :left_control => KbLeftControl,
      :left_shift => KbLeftShift,
      :numpad_0 => KbNumpad0,
      :numpad_1 => KbNumpad1,
      :numpad_2 => KbNumpad2,
      :numpad_3 => KbNumpad3,
      :numpad_4 => KbNumpad4,
      :numpad_5 => KbNumpad5,
      :numpad_6 => KbNumpad6,
      :numpad_7 => KbNumpad7,
      :numpad_8 => KbNumpad8,
      :numpad_9 => KbNumpad9,
      :numpad_add => KbNumpadAdd,
      :numpad_divide => KbNumpadDivide,
      :numpad_multiply => KbNumpadMultiply,
      :numpad_subtract => KbNumpadSubtract,
      :page_down => KbPageDown,
      :page_up => KbPageUp,
      # FIXME: KbPause undefined??
      #:pause => KbPause, 
      :return => KbReturn,
      :right => KbRight,
      :right_alt => KbRightAlt,
      :right_control => KbRightControl,
      :right_shift => KbRightShift,
      :space => KbSpace,
      :tab => KbTab,
      :up => KbUp,
      :ms_left => MsLeft,
      :ms_middle => MsMiddle,
      :ms_right => MsRight,
      :ms_wheel_down => MsWheelDown,
      :ms_wheel_up => MsWheelUp,
      :gp_0 => GpButton0,
      :gp_1 => GpButton1,
      :gp_10 => GpButton10,
      :gp_11 => GpButton11,
      :gp_12 => GpButton12,
      :gp_13 => GpButton13,
      :gp_14 => GpButton14,
      :gp_15 => GpButton15,
      :gp_2 => GpButton2,
      :gp_3 => GpButton3,
      :gp_4 => GpButton4,
      :gp_5 => GpButton5,
      :gp_6 => GpButton6,
      :gp_7 => GpButton7,
      :gp_8 => GpButton8,
      :gp_9 => GpButton9,
      :gp_down => GpDown,
      :gp_left => GpLeft,
      :gp_right => GpRight,
      :gp_up => GpUp
    }
  end

  class Color
    class << self
      def black
        @black ||= Color.new 255, 0, 0, 0
      end
      
      def blue
         @blue ||= Color.new 255, 0, 0, 255
      end
      
      def green
         @green ||= Color.new 255, 0, 255, 0
      end
      
      def orange
         @orange ||= Color.new 255, 255, 255, 0
      end
      
      def pink
         @pink ||= Color.new 255, 255, 0, 255
      end
      
      def red
         @red ||= Color.new 255, 255, 0, 0
      end
      
      def turquoise
         @turquoise ||= Color.new 255, 0, 255, 255
      end
      
      def white
        @white ||= Color.new 255, 255, 255, 255
      end
      
      def yellow
         @yellow ||= Color.new 255, 255, 255, 0
      end
    end
  end
  
  class Image
    def draw_tiled(x, y, w, h, z, options={})
      options[:tile_width] ||= 32
      options[:tile_height] ||= 32
      (h / options[:tile_height]).times do |ty|
        (w / options[:tile_width]).times do |tx|
          draw x + tx * options[:tile_width], y + ty * options[:tile_height], z
        end
      end
    end

    def draw_as_rect(x, y, w, h, z, options={})
      @@white ||= Color.new(255, 255, 255, 255)
      options[:color] ||= @@white
      draw_as_quad x, y, options[:color], x + w, y, options[:color], x, y + h, options[:color], x + w, y + h, options[:color], z
    end
  end

  class Window
    alias_method :old_button_down?, :button_down?
    def button_down?(id)
      id = sym_to_button_id id if id.class == Symbol
      old_button_down? id
    end
    
    alias_method :old_initialize, :initialize
    def initialize(width, height, fullscreen, update_interval=16.666666)
      old_initialize(width, height, fullscreen, update_interval)
      $window = self
    end
    
    def draw_pixel(x, y, color, z=0)
      draw_rect x, y, 1, 1, color, z
    end
    
    def draw_rect(x, y, w, h, color, z=0)
      draw_quad x, y, color, x + w, y, color, x, y + h, color, x + w, y + h, color, z
    end
    
    def sym_to_button_id(sym)
      Button::SYM_TO_ID[sym] || char_to_button_id(sym.to_s)
    end
  end
end

class Symbol
  def to_btn
    $window.sym_to_button_id self
  end
end